/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.personnel.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.mybatis.SpliceQueryWrapper;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.personnel.api.constant.CertificateConstant;
import com.fuyao.cloud.personnel.api.entity.CertificateInfo;
import com.fuyao.cloud.personnel.api.entity.CertificateLendRecord;
import com.fuyao.cloud.personnel.api.vo.CertificateInfoSearchVO;
import com.fuyao.cloud.personnel.api.vo.excel.CertificateInfoExcelVO;
import com.fuyao.cloud.personnel.service.CertificateInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 证件信息
 *
 * @author 碎月
 * @date 2022-01-12 15:47:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/certificateinfo" )
@Api(value = "certificateinfo", tags = "证件信息管理")
public class CertificateInfoController {

    private final CertificateInfoService certificateInfoService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param certificateInfo 证件信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_view')" )
    public R getCertificateInfoPage(Page page, CertificateInfoSearchVO certificateInfo) {

        return R.ok(certificateInfoService.page(page, SpliceQueryWrapper.spliceSearchCondition(certificateInfo)));
    }


    /**
     * 通过id查询证件信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(certificateInfoService.getById(id));
    }

    /**
     * 新增证件信息
     * @param certificateInfo 证件信息
     * @return R
     */
    @ApiOperation(value = "新增证件信息", notes = "新增证件信息")
    @SysLog("新增证件信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_add')" )
    public R save(@RequestBody CertificateInfo certificateInfo) {
        return R.ok(certificateInfoService.save(certificateInfo));
    }

    /**
     * 修改证件信息
     * @param certificateInfo 证件信息
     * @return R
     */
    @ApiOperation(value = "修改证件信息", notes = "修改证件信息")
    @SysLog("修改证件信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_edit')" )
    public R updateById(@RequestBody CertificateInfo certificateInfo) {
        return R.ok(certificateInfoService.updateById(certificateInfo));
    }

    /**
     * 通过id删除证件信息
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除证件信息", notes = "通过id删除证件信息")
    @SysLog("通过id删除证件信息" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(certificateInfoService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param certificateInfo 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @PostMapping("/export")
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_export')" )
    public List<CertificateInfoExcelVO> export(CertificateInfo certificateInfo) {
        List<CertificateInfo> certificateInfoList = certificateInfoService.list(Wrappers.query(certificateInfo));
        if(CollUtil.isNotEmpty(certificateInfoList)){
            return certificateInfoList.stream().map(certificateInfoTemp -> {
                CertificateInfoExcelVO excelVO = new CertificateInfoExcelVO();
                BeanUtil.copyProperties(certificateInfoTemp,excelVO);
                return excelVO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    /**
     * 导入
     * @return R
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('personnel_certificateinfo_export')")
    public R importUser(@RequestExcel List<CertificateInfoExcelVO> excelVOList, BindingResult bindingResult) {
        return certificateInfoService.importCertificateInfo(excelVOList, bindingResult);
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param certificateInfoId 证件信息id
     * @return
     */
    @ApiOperation(value = "证件信息记录分页查询", notes = "分页查询")
    @GetMapping("/lendRecord/page/{certificateInfoId}" )
    public R getCertifiLenRecordPage(Page page, @PathVariable("certificateInfoId") Long certificateInfoId) {
        return R.ok(certificateInfoService.lendRecordPage(page, certificateInfoId));
    }

    /**
     * 借用
     */
    @PostMapping("/lend")
    public R<Integer> lend(@RequestBody CertificateLendRecord lendRecord){
        Integer res= this.certificateInfoService.lend(lendRecord);
        return getR(res);
    }

    /**
     * 归还
     */
    @GetMapping("/giveBack/{id}")
    public R giveBack(@PathVariable("id") Long id){
        Integer res =this.certificateInfoService.giveBack(id);
        return getR(res);
    }

    private R getR(int res){
        if (res == CertificateConstant.SUCCESS_CODE){
            return R.ok(CertificateConstant.SUCCESS_MSG);
        }else if (res == CertificateConstant.FAILD_NULL_CODE){
            return R.failed(CertificateConstant.FAILD_NULL_MSG);
        }else if (res == CertificateConstant.FAILD_ID_CODE){
            return R.failed(CertificateConstant.FAILD_ID_MSG);
        }
        return R.ok();
    }
}
